<?
//Überprüfen ob der Abruf über die Hauptseite erfolgt
if(!defined('InStern')) exit;


include("conf/forum.conf.php");
$seite=secstr($_GET[seite],5);
if(isset($_GET[search])) $_SESSION[marked]=secstr($_GET[search]);
else $_SESSION[marked]="";

//Notfalls wird der ungelesen Array als Array gesetzt
if(!isset($_GET[search])) if(gettype($_SESSION[ungelesen][$_GET[forum]])!="array") settype($_SESSION[ungelesen][$_GET[forum]],"array");
//Dann wird das Forum geladen
$db->query("SELECT * FROM foren WHERE Id='".secstr($_GET[forum],5)."';");
if(isset($_GET[search]) OR $forum=$db->fetch_assoc())
{
  if(!isset($_GET[search])) include("sc/admincheck.sc.php");
  else
  {  //Fall wenn die suche ausgelöst wurde
	$forum[Name]=$lang[topics_searchresult];
  }
  //ADMINISTRATION - BEITRAG LÖSCHEN
  if($_GET['do']=="erasetopic" AND $admin)
  {
    $result=$db->query("DELETE FROM topics WHERE Forum=$forum[Id] AND Id=$_GET[topic];");
    if($db->affected_rows()>0)
    {
      $db->query("DELETE FROM posts WHERE Topic=$_GET[topic];");
      $db->query("UPDATE foren SET Beitraege=Beitraege-".$db->affected_rows().",Themen=Themen-1 WHERE ID='$forum[Id]';");
      echo $lang[topics_erased];
    }
    else 
      echo $lang[topics_notfound];
  }
  //Stylesheet laden
  echo "<link href=\"$template/css/forum.css\" rel=\"stylesheet\" type=\"text/css\" />";
  //Header des Forums
  echo "<h1>$forum[Name]</h1>";
  //Administratorinformation
  if($admin) echo "<div class=\"info\">$lang[topics_youareadmin]</div>";
  //Breadcrumb
  echo "
  <table class=\"forum_options\">
   <tr> 
    <td>
     <input type=\"button\" value=\"$lang[topics_newtopic]\" onclick=\"location.href='$_SERVER[PHP_SELF]?forum=$forum[Id]&page=newtopic'\"";  if(isset($_GET[search])) echo " disabled=\"disabled\""; echo "/>
    </td>
    <th width=\"80%\">
     <a href=\"$_SERVER[PHP_SELF]?page=forum\">Forum</a> ";  if(!isset($_GET[search]))  echo "-> <a href=\"$_SERVER[PHP_SELF]?page=topics&forum=$forum[Id]\">$forum[Name]</a>"; echo "
    </th>
    <th>
     <input "; 
     if(!isset($_GET[search])) echo "value=\"$lang[topics_search]\"";
     else echo "value=\"".secstr($_GET[search])."\"";
     echo " onclick=\"if(this.value=='suche') this.value='';\" onblur=\"if(this.value=='') this.value='$lang[topics_search]';\" onkeypress=\"if(event.keyCode==13) location.href='index.php?page=topics&search='+this.value;\"/>
    </th>
   </tr>
  </table>";
  

  echo "<table class=\"forum\">
   <tr class=\"forum_kat\">
    <th>&nbsp;</th>
    <th>$lang[topics_topic]</th>
    <th>$lang[topics_Author]</th>
    <th>$lang[topics_topiccount]</th>
    <th>$lang[topics_hits]</th>
    <th>$lang[topics_last]</th>";
    if($admin) echo "<th>&nbsp;</th>";    
    echo "
   </tr>";
  if(!isset($_GET[search])) $result=$db->query("SELECT * FROM topics WHERE Forum='$forum[Id]' ORDER BY Prio DESC, Last DESC LIMIT ".($seite*$config_forum[topics]).",".$config_forum[topics].";");
  else 
  {
    $forum[Themen]=$db->num_rows($db->query("SELECT Topic FROM posts WHERE Text like '%".secstr($_GET[search])."%' GROUP BY Topic"));
    $result=$db->query("SELECT topics.*,posts.Id AS FoundId FROM topics,posts WHERE topics.Id=posts.Topic AND Text like '%".secstr($_GET[search])."%' ORDER BY topics.Last DESC LIMIT ".($seite*$config_forum[topics]).",".$config_forum[topics].";");
  }  
  while($topic=$db->fetch_assoc($result))
  {
    if(isset($_GET[search])) 
    { 
      $klasse="gelesen";
      $add="&post=$topic[FoundId]";
    }
    else
    {
      if($topic[Last]>$_SESSION[last] AND !in_array($topic[Id], $_SESSION[ungelesen][$forum[Id]])) $klasse="ungelesen";
      else $klasse="gelesen";
    }
    if($_SESSION[user][Admin]>=$topic[AllowRead])
    {
      if($topic[Closed]==1)
        $prio="<img src=\"images/forum_closed.png\"  alt=\"$lang[topics_closed]\" />";
      elseif($topic[Prio]>0)
        $prio="<img src=\"images/forum_prio$topic[Prio].png\"  alt=\"".$config[prio][$topic[Prio]]."\" />";
      else
        $prio="&nbsp;";
      echo "
      <tr class=\"beitrag\">
       <td class=\"$klasse\" onclick=\"location.href='$_SERVER[PHP_SELF]?page=viewtopic&topic=$topic[Id]$add';\" align=\"center\" valign=\"center\">$prio </td>
       <td style=\"width:350px;\" onclick=\"location.href='$_SERVER[PHP_SELF]?page=viewtopic&topic=$topic[Id]$add';\" >$topic[Betreff]</td>
       <td align=\"center\" onclick=\"location.href='$_SERVER[PHP_SELF]?page=viewtopic&topic=$topic[Id]$add';\">$topic[Autor]</td>
       <td align=\"center\" onclick=\"location.href='$_SERVER[PHP_SELF]?page=viewtopic&topic=$topic[Id]$add';\">$topic[Beitraege]</td>
       <td align=\"center\" onclick=\"location.href='$_SERVER[PHP_SELF]?page=viewtopic&topic=$topic[Id]$add';\">$topic[Gelesen]</td>
       <td align=\"center\" style=\"font-size:10px\" onclick=\"location.href='$_SERVER[PHP_SELF]?page=viewtopic&topic=$topic[Id]&post=last$add';\">".date("d.m.Y - H:i:s", $topic[Last])."</td>";
       if($admin) echo "<th><input type=\"button\" value=\"X\" onclick=\"if(confirm('$lang[topics_confirmerase]')) location.href='$_SERVER[PHP_SELF]?page=$_SESSION[page]&forum=$forum[Id]&do=erasetopic&topic=$topic[Id]';\" /></th>";
       echo "
      </tr>";
    }
  }
  echo "</table>
  <table class=\"forum_options\" style=\"margin-top:17px;\">
   <tr>
    <td>
     <input type=\"button\" value=\"$lang[topics_new_topic]\" onclick=\"location.href='$_SERVER[PHP_SELF]?forum=$forum[Id]&page=newtopic'\"";if(isset($_GET[search])) echo " disabled=\"disabled\""; echo "/>
    </td>
    <th width=\"90%\">";
    if(isset($_GET[search])) 
      echo leiste("$_SERVER[PHP_SELF]?page=$_SESSION[page]&search=".secstr($_GET[search]),ceil($forum[Themen]/$config_forum[topics]),$seite);
    else
      echo leiste("$_SERVER[PHP_SELF]?page=$_SESSION[page]&forum=$forum[Id]",ceil($forum[Themen]/$config_forum[topics]),$seite);
     echo "  
    </th>
   </tr>
  </table>";
}
else echo $lang[topics_forumnotfound];